﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Helpers;
using HumanFace;
using Svg;
using System.Xml;
using System.Threading;
using System.Reflection;

namespace StasmApp
{
    class Program
    {
        private static bool test = false;
        static void Main(string[] args)
        {
            if (test)
            {
                args = new string[3];
                args[0] = "svg";
                args[1] = @"D:\Projects\SAME\StasmTCPServer\StasmApp\OImage\1669863716.jpg";
                args[2] = @"D:\Projects\SAME\StasmTCPServer\StasmApp\StasmData\";
            }
                try
            {

                if (args.Length == 0)
                    return;
                switch (args[0])
                {
                    case "svg":
                        if (!String.IsNullOrEmpty(args[2]))
                        {
                            Console.WriteLine("SVG:");
                            Console.WriteLine(args.Length);
                            Console.WriteLine(args[1]);
                            Console.WriteLine(args[2]);
                            if (args.Length != 3) break;
                            if (File.Exists(args[1]))
                            {
                                Console.WriteLine("File Exists:");
                                Dictionary<string, string> templates = GetTemplates();
                                StasmHelper sHelper = new StasmHelper();
                                Console.WriteLine("GettedTemplates");
                                //Console.WriteLine("templates.Count: {0}",templates.Count);
                                Console.WriteLine("Eye: {0}", templates["Eye"]);
                                //Console.WriteLine(Path.Combine(Path.GetDirectoryName(args[1]), "output" + Path.GetFileNameWithoutExtension(args[1]) + ".svg"));
                                Face face = sHelper.GetHumanFace(args[1], args[2], templates);
                                Console.WriteLine("Faced");
                                //Console.WriteLine(face.SvgDoc);
                                string svgPath = Path.Combine(Path.GetDirectoryName(args[1]), Path.GetFileNameWithoutExtension(args[1]) + ".svg");
                                Console.WriteLine(svgPath);
                                //Console.WriteLine(face.SvgDoc);
                                StreamWriter sWriter = new StreamWriter(svgPath);
                                sWriter.Write(face.SvgDoc);
                                Console.WriteLine(svgPath);
                                sWriter.Close();
                            }
                        }
                        break;
                    case "jpg":
                        break;
                    case "help":
                        Help();
                        break;
                    default:
                        if (String.IsNullOrEmpty(args[1])) Help();
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        private static Dictionary<string, string> GetTemplates()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add(typeof(Ear).Name, GetSVG(typeof(Ear)));
            dictionary.Add(typeof(Eye).Name, GetSVG(typeof(Eye)));
            dictionary.Add(typeof(EyeBrow).Name, GetSVG(typeof(EyeBrow)));
            dictionary.Add(typeof(Face).Name, GetSVG(typeof(Face)));
            dictionary.Add(typeof(Nose).Name, GetSVG(typeof(Nose)));
            dictionary.Add(typeof(Lips).Name, GetSVG(typeof(Lips)));
            dictionary.Add(typeof(Visage).Name, GetSVG(typeof(Visage)));
            return dictionary;
        }
        public static string GetSVG(Type humanFaceType)
        {
            //DB Tabloları oluşturulduğunda yeniden düzenlenecek.
            string imagePath = String.Empty;
            if (test) imagePath = Environment.CurrentDirectory + @"\ImageLibrary\";
            else imagePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)  + @"\ImageLibrary\";
            
            string document = null;
            if (!String.IsNullOrEmpty(imagePath))
                try
                {
                    Console.WriteLine(String.Format("{0}{1}.svg", imagePath, humanFaceType.Name));
                    SvgDocument svdoc = SvgDocument.Open(String.Format("{0}{1}.svg", imagePath, humanFaceType.Name));
                    using (StringWriter str = new StringWriter())
                    using (XmlTextWriter xml = new XmlTextWriter(str))
                    {
                        svdoc.WriteElement(xml);
                        document = str.ToString();
                    }

                }
                catch (FileNotFoundException ex)
                {
                    //Dosya bulunamadı.
                }

            return document;
        }
        private static void Help()
        {
            Console.WriteLine("Argument is Image Path");
        }
    }
}
